/*
 * Copyright (C) 2010 The Android Open Source Project 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

package org.lhlzi.ithink;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.os.Bundle;
import android.util.Log;

public class DataCacheWrapper {
	
	public final static String TAG = DataCacheWrapper.class.toString();
	
	public final static String UNKNOW_STATE = "unknown";
	
	public final static String MN_REGU = "^1[3,5,8]\\d{9}$";
	public final static String LLN_REGU = "^((0\\d{2,4})|[8,9]\\d{2})\\d{7,8}$";
	
	protected static HashMap<String, String> states = new HashMap<String, String>();
	
	private static DataCacheWrapper dataCacheWrapper;
	
	private static SQLLocalStorage localStorage = SQLLocalStorage.getInstance();
	
	
//	private int mPrefixLength;
	
	
	private DataCacheWrapper() {
		fillStates();
	}
	
	public static DataCacheWrapper getInstance() {
		if (dataCacheWrapper == null ) {
			dataCacheWrapper = new DataCacheWrapper();
		}
		
		return dataCacheWrapper;
	}
	
	public static DataCacheWrapper resetDataCacheWrapper() {
		//dataCacheWrapper = null;
		
		localStorage = SQLLocalStorage.resetLocalStorage();
		
//		Log.i(TAG, "Call resetDataCacheWrapper();");
		
		return getInstance();
	}
	
//	public int formatChinaNumber(String landline) {
//		landline = landline.replace("[^0-9]", "");
//		boolean found = false;
//		String fix = null;
//		
//		found = isLLN(landline);
//		
//		if (found) {
//			fix = landline.substring(0, 5);
//			
//			if (!states.containsKey(fix)) {
//				fix = landline.substring(0, 4);
//				
//				if (!states.containsKey(fix)) {
//					fix = landline.substring(0, 3);
//				}
//			}
//		}
//		
//		return fix == null ? 0 : fix.length();
//	}
	
	public String revealNumber(String number) {
		
		String phone = number.replace("[^0-9]", "");
		
		int length = phone.length();
		
		if (length > 12 || length < 3) { // 不考虑国际长途
			return DataCacheWrapper.UNKNOW_STATE;
		}

		Bundle extras = null;
		boolean found = false;
		
		found = isMN(phone); // check number if mobile phone.
		
		if (localStorage != null && found) {
			
			extras = localStorage.get(Integer.parseInt(phone.substring(0, 7)));
			
			if (extras != null && extras.containsKey(SQLLocalStorage.LOCATION_COLUMN))
				return extras.getString(SQLLocalStorage.LOCATION_COLUMN);
//			else if (extras != null && extras.containsKey(SQLLocalStorage.TYPE_COLUMN))
//				return extras.getString(SQLLocalStorage.TYPE_COLUMN);
		}
		
		
		found = isLLN(phone); // check number if land telephone.
		
		if (found && states != null && length > 3) { // 3 digits
			if (states.containsKey(phone.substring(0, 3)))
				return states.get(phone.substring(0, 3));
			else if (states.containsKey(phone.substring(0, 4)))
				return states.get(phone.substring(0, 4));
			else if (states.containsKey(phone.substring(0, 5)))
				return states.get(phone.substring(0, 5));
			
		} else {
			// TO DO SOMETHING.
		}
		
		return DataCacheWrapper.UNKNOW_STATE;
	}
	
	public boolean isMN(String number) {
		return match(DataCacheWrapper.MN_REGU, number);
	}
	
	public boolean isLLN(String number) {
		return match(DataCacheWrapper.LLN_REGU, number);
	}

	private boolean match(String pattern, String number) {
		
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(number);
		return m.find();
	}
	
	
	
	protected void fillStates()
    {
        // 5位区号
        states.put("04802", "内蒙古 扎兰屯");
        states.put("04803", "内蒙古 鄂伦春");
        states.put("04804", "内蒙古 陈巴尔虎旗");
        states.put("04806", "内蒙古 新巴尔虎右旗");
        states.put("04831", "内蒙古 阿拉善左旗");
        states.put("04841", "内蒙古 乌兰察布盟");
        states.put("04845", "内蒙古 察哈尔右翼前旗");
        states.put("04846", "内蒙古 察哈尔右翼中旗");
        states.put("04847", "内蒙古 察哈尔右翼后旗");
        states.put("04850", "内蒙古 科尔沁右翼中旗");
        states.put("04851", "内蒙古 开鲁县");
        states.put("04853", "内蒙古 奈曼旗");
        states.put("04854", "内蒙古 扎鲁特旗");
        states.put("04857", "内蒙古 霍林郭勒");
        states.put("04858", "内蒙古 突泉县");
        states.put("04859", "内蒙古 扎赉特旗");
        states.put("04871", "内蒙古 达拉特旗");
        states.put("04872", "内蒙古 伊金霍洛旗");
        states.put("04874", "内蒙古 杭锦旗");
        states.put("04875", "内蒙古 乌审旗");
        states.put("04876", "内蒙古 鄂托克旗");
        states.put("04877", "内蒙古 鄂托克前旗");
        states.put("04881", "内蒙古 五原县");
        states.put("04883", "内蒙古 乌拉特前旗");
        states.put("04884", "内蒙古 乌拉特中旗");
        states.put("04887", "内蒙古 乌拉特后旗");
        states.put("04891", "内蒙古 太仆寺旗");
        states.put("04892", "内蒙古 苏尼特左旗");
        states.put("04893", "内蒙古 苏尼特右旗");
        states.put("04895", "内蒙古 东乌珠穆沁旗");
        states.put("04896", "内蒙古 西乌珠穆沁旗");
        states.put("08030", "西藏 山南");
        states.put("08036", "西藏 山南");
        states.put("08038", "西藏 山南");
        states.put("08047", "西藏 山南");
        states.put("08069", "西藏 阿里");
        states.put("08071", "西藏 阿里");
        states.put("08077", "西藏 山南");
        states.put("08081", "西藏 尼玛县");
        states.put("08241", "四川 凉山彝族自治州");
        states.put("08260", "四川 达州");
        states.put("08271", "四川 达州");
        states.put("08272", "四川 达州");
        states.put("08273", "四川 达州");
        states.put("08274", "四川 达州");
        states.put("08275", "四川 达州");
        states.put("08299", "重庆 黔江");
        states.put("08407", "四川 阿坝藏族羌族自治州");
        states.put("08417", "四川 凉山彝族自治州");
        states.put("08440", "四川 凉山彝族自治州");
        states.put("08444", "四川 凉山彝族自治州");
        states.put("08445", "四川 凉山彝族自治州");
        states.put("08446", "四川 凉山彝族自治州");
        states.put("08447", "四川 凉山彝族自治州");
        states.put("08455", "四川 凉山彝族自治州");
        states.put("08456", "四川 凉山彝族自治州");
        states.put("08458", "四川 凉山彝族自治州");
        states.put("08459", "四川 凉山彝族自治州");
        states.put("08489", "四川 阿坝藏族羌族自治州");
        states.put("08490", "四川 阿坝藏族羌族自治州");
        states.put("08491", "四川 阿坝藏族羌族自治州");
        states.put("08492", "四川 阿坝藏族羌族自治州");
        states.put("08493", "四川 阿坝藏族羌族自治州");
        states.put("08495", "四川 阿坝藏族羌族自治州");
        states.put("08497", "四川 阿坝藏族羌族自治州");
        states.put("08498", "四川 阿坝藏族羌族自治州");
        states.put("08499", "四川 阿坝藏族羌族自治州");
        states.put("09093", "新疆 博尔塔拉蒙古自治州");
        states.put("09820", "青海 黄南藏族自治州");
        states.put("09829", "青海 黄南藏族自治州");
        states.put("09831", "青海 海西蒙古族自治州");
        states.put("09832", "青海 海西蒙古族自治州");
        states.put("09833", "青海 海西蒙古族自治州");
        states.put("09834", "青海 玉树藏族自治州");
        states.put("09835", "青海 玉树藏族自治州");
        states.put("09837", "青海 玉树藏族自治州");
        states.put("09838", "青海 玉树藏族自治州");
        states.put("09841", "青海 海南藏族自治州");
        states.put("09842", "青海 海南藏族自治州");
        states.put("09844", "青海 海南藏族自治州");
        states.put("09846", "青海 海北藏族自治州");
        states.put("09847", "青海 海北藏族自治州");
        states.put("09851", "青海 果洛藏族自治州");
        states.put("09852", "青海 果洛藏族自治州");
        states.put("09853", "青海 果洛藏族自治州");
        states.put("09854", "青海 果洛藏族自治州");
        states.put("09855", "青海 果洛藏族自治州");
        // 3位区号
        states.put("010", "北京");
        states.put("020", "广东 广州");
        states.put("021", "上海");
        states.put("022", "天津");
        states.put("023", "重庆");
        states.put("024", "辽宁 沈阳");
        states.put("025", "江苏 南京");
        states.put("027", "湖北 武汉");
        states.put("028", "四川 成都");
        states.put("029", "陕西 西安");
        states.put("852", "香港");
        states.put("853", "澳门");
        states.put("886", "台湾");
        states.put("887", "台湾");
        states.put("888", "台湾");
        states.put("889", "台湾");
        states.put("890", "台湾");
        states.put("891", "台湾");
        states.put("892", "台湾");
        states.put("893", "台湾");
        states.put("894", "台湾");
        states.put("895", "台湾");
        states.put("896", "台湾");
        states.put("897", "台湾");
        states.put("898", "台湾");
        states.put("899", "台湾");
        states.put("900", "台湾");
        states.put("901", "台湾");
        states.put("902", "台湾");
        states.put("903", "台湾");
        states.put("904", "台湾");
        states.put("905", "台湾");
        states.put("906", "台湾");
        states.put("907", "台湾");
        states.put("908", "台湾");
        states.put("909", "台湾");
        states.put("910", "台湾");
        states.put("911", "台湾");
        states.put("912", "台湾");
        states.put("913", "台湾");
        // 4位区号
        states.put("0310","河北 邯郸");
        states.put("0311","河北 石家庄");
        states.put("0312","河北 保定");
        states.put("0313","河北 张家口");
        states.put("0314","河北 承德");
        states.put("0315","河北 唐山");
        states.put("0316","河北 廊坊");
        states.put("0317","河北 沧州");
        states.put("0318","河北 衡水");
        states.put("0319","河北 邢台");
        states.put("0335","河北 秦皇岛");
        states.put("0349","山西 朔州(或右玉镇)");
        states.put("0350","山西 忻州");
        states.put("0351","山西 太原");
        states.put("0352","山西 大同");
        states.put("0353","山西 阳泉");
        states.put("0354","山西 晋中(或榆次市)");
        states.put("0355","山西 长治");
        states.put("0356","山西 晋城");
        states.put("0357","山西 临汾");
        states.put("0358","山西 吕梁(或汾阳市)");
        states.put("0359","山西 运城");
        states.put("0370","河南 商丘");
        states.put("0371","河南 郑州");
        states.put("0372","河南 安阳");
        states.put("0373","河南 新乡");
        states.put("0374","河南 许昌");
        states.put("0375","河南 平顶山");
        states.put("0376","河南 信阳");
        states.put("0377","河南 南阳");
        states.put("0378","河南 开封");
        states.put("0379","河南 洛阳");
        states.put("0391","河南 焦作(或济源)");
        states.put("0392","河南 鹤壁");
        states.put("0393","河南 濮阳");
        states.put("0394","河南 周口");
        states.put("0395","河南 漯河");
        states.put("0396","河南 驻马店");
        states.put("0398","河南 三门峡");
        states.put("0410","辽宁 铁岭");
        states.put("0411","辽宁 大连");
        states.put("0412","辽宁 鞍山");
        states.put("0413","辽宁 抚顺");
        states.put("0414","辽宁 本溪");
        states.put("0415","辽宁 丹东");
        states.put("0416","辽宁 锦州");
        states.put("0417","辽宁 营口");
        states.put("0418","辽宁 阜新");
        states.put("0419","辽宁 辽阳");
        states.put("0421","辽宁 朝阳");
        states.put("0427","辽宁 盘锦");
        states.put("0429","辽宁 葫芦岛");
        states.put("0431","吉林 长春");
        states.put("0432","吉林 吉林");
        states.put("0433","吉林 延边朝鲜族自治州");
        states.put("0434","吉林 四平");
        states.put("0435","吉林 通化");
        states.put("0436","吉林 白城");
        states.put("0437","吉林 辽源");
        states.put("0438","吉林 松原");
        states.put("0439","吉林 白山");
        states.put("0451","黑龙江 哈尔滨");
        states.put("0452","黑龙江 齐齐哈尔");
        states.put("0453","黑龙江 牡丹江");
        states.put("0454","黑龙江 佳木斯");
        states.put("0455","黑龙江 绥化");
        states.put("0456","黑龙江 黑河");
        states.put("0457","黑龙江 大兴安岭");
        states.put("0458","黑龙江 伊春");
        states.put("0459","黑龙江 大庆");
        states.put("0464","黑龙江 七台河");
        states.put("0467","黑龙江 鸡西");
        states.put("0468","黑龙江 鹤岗");
        states.put("0469","黑龙江 双鸭山");
        states.put("0470","内蒙古");
        states.put("0471","内蒙古");
        states.put("0472","内蒙古 包头");
        states.put("0473","内蒙古 乌海");
        states.put("0474","内蒙古");
        states.put("0475","内蒙古");
        states.put("0476","内蒙古");
        states.put("0477","内蒙古 东胜(或准格尔旗)");
        states.put("0478","内蒙古");
        states.put("0479","内蒙古");
        states.put("0482","内蒙古 乌兰浩特市");
        states.put("0483","内蒙古 阿拉善右旗");
        states.put("0510","江苏 无锡");
        states.put("0511","江苏 镇江");
        states.put("0512","江苏 苏州");
        states.put("0513","江苏 南通");
        states.put("0514","江苏 扬州");
        states.put("0515","江苏 盐城");
        states.put("0516","江苏 徐州");
        states.put("0517","江苏 淮阴");
        states.put("0518","江苏 连云港");
        states.put("0519","江苏 常州");
        states.put("0523","江苏 泰州");
        states.put("0527","江苏 宿迁");
        states.put("0530","山东 菏泽");
        states.put("0531","山东 济南");
        states.put("0532","山东 青岛");
        states.put("0533","山东 淄博");
        states.put("0534","山东 德州");
        states.put("0535","山东 烟台");
        states.put("0536","山东 潍坊");
        states.put("0537","山东 济宁");
        states.put("0538","山东 泰安");
        states.put("0539","山东 临沂");
        states.put("0543","山东 滨州");
        states.put("0546","山东 东营");
        states.put("0550","安徽 滁州");
        states.put("0551","安徽 合肥");
        states.put("0552","安徽 蚌埠");
        states.put("0553","安徽 芜湖");
        states.put("0554","安徽 淮南");
        states.put("0555","安徽 马鞍山");
        states.put("0556","安徽 安庆");
        states.put("0557","安徽 宿州");
        states.put("0558","安徽 亳州(或阜阳)");
        states.put("0559","安徽 黄山");
        states.put("0561","安徽 淮北");
        states.put("0562","安徽 铜陵");
        states.put("0563","安徽 宣州(或宣城)");
        states.put("0564","安徽 六安");
        states.put("0565","安徽 巢湖");
        states.put("0566","安徽 贵池(或池州)");
        states.put("0570","浙江 衢州");
        states.put("0571","浙江 杭州");
        states.put("0572","浙江 湖州");
        states.put("0573","浙江 嘉兴");
        states.put("0574","浙江 宁波");
        states.put("0575","浙江 绍兴");
        states.put("0576","浙江 台州");
        states.put("0577","浙江 温州");
        states.put("0578","浙江 丽水");
        states.put("0579","浙江 金华");
        states.put("0580","浙江 舟山");
        states.put("0591","福建 福州");
        states.put("0592","福建 厦门");
        states.put("0593","福建 宁德");
        states.put("0594","福建 莆田");
        states.put("0595","福建 泉州");
        states.put("0596","福建 漳州");
        states.put("0597","福建 龙岩");
        states.put("0598","福建 三明");
        states.put("0599","福建 南平");
        states.put("0631","山东 威海");
        states.put("0632","山东 枣庄");
        states.put("0633","山东 日照");
        states.put("0634","山东 莱芜");
        states.put("0635","山东 聊城");
        states.put("0660","广东 汕尾");
        states.put("0662","广东 阳江");
        states.put("0663","广东 揭阳");
        states.put("0668","广东 茂名");
        states.put("0691","云南 西双版纳傣族自治州");
        states.put("0692","云南 德宏傣族景颇族自治州");
        states.put("0701","江西 鹰潭");
        states.put("0710","湖北 襄樊");
        states.put("0711","湖北 鄂州");
        states.put("0712","湖北 孝感");
        states.put("0713","湖北 黄冈");
        states.put("0714","湖北 黄石");
        states.put("0715","湖北 咸宁");
        states.put("0716","湖北 荆州");
        states.put("0717","湖北 宜昌");
        states.put("0718","湖北 恩施土家族苗族自治州");
        states.put("0719","湖北 十堰(或神农架林区)");
        states.put("0722","湖北 随州");
        states.put("0724","湖北 荆门");
        states.put("0728","湖北 天门(或潜江或仙桃)");
        states.put("0730","湖南 岳阳");
        states.put("0731","湖南 长沙");
        states.put("0732","湖南 湘潭");
        states.put("0733","湖南 株洲");
        states.put("0734","湖南 衡阳");
        states.put("0735","湖南 宜章(或郴州)");
        states.put("0736","湖南 常德");
        states.put("0737","湖南 益阳");
        states.put("0738","湖南 娄底");
        states.put("0739","湖南 邵阳");
        states.put("0743","湖南 湘西土家族苗族自治州");
        states.put("0744","湖南 张家界");
        states.put("0745","湖南 怀化");
        states.put("0746","湖南 永州");
        states.put("0750","广东 江门");
        states.put("0751","广东 韶关");
        states.put("0752","广东 惠州");
        states.put("0753","广东 梅州");
        states.put("0754","广东 汕头");
        states.put("0755","广东 深圳");
        states.put("0756","广东 珠海");
        states.put("0757","广东 佛山");
        states.put("0758","广东 肇庆");
        states.put("0759","广东 湛江");
        states.put("0760","广东 中山");
        states.put("0762","广东 河源");
        states.put("0763","广东 清远");
        states.put("0766","广东 云浮");
        states.put("0768","广东 潮州");
        states.put("0769","广东 东莞");
        states.put("0770","广西 防城港");
        states.put("0771","广西 南宁");
        states.put("0772","广西 柳州");
        states.put("0773","广西 桂林");
        states.put("0774","广西 梧州(或贺州)");
        states.put("0775","广西 玉林(或贵港）");
        states.put("0776","广西 百色");
        states.put("0777","广西 钦州");
        states.put("0778","广西 河池");
        states.put("0779","广西 北海");
        states.put("0790","江西 新余");
        states.put("0791","江西 南昌");
        states.put("0792","江西 九江");
        states.put("0793","江西 上饶");
        states.put("0794","江西 抚州");
        states.put("0795","江西 宜春");
        states.put("0796","江西 吉安");
        states.put("0797","江西 赣州");
        states.put("0798","江西 景德镇");
        states.put("0799","江西 萍乡");
        states.put("0812","四川 攀枝花");
        states.put("0813","四川 自贡");
        states.put("0816","四川 绵阳");
        states.put("0817","四川 南充");
        states.put("0818","四川 达州(或达川市)");
        states.put("0825","四川 遂宁");
        states.put("0826","四川 广安");
        states.put("0827","四川 巴中");
        states.put("0830","四川 泸州");
        states.put("0831","四川 宜宾");
        states.put("0832","四川 资阳(或内江)");
        states.put("0833","四川 眉山(或乐山)");
        states.put("0834","四川 凉山彝族自治州");
        states.put("0835","四川 雅安(或甘孜藏族自治州)");
        states.put("0836","甘藏族自治州");
        states.put("0837","四川 西昌市");
        states.put("0838","四川 德阳");
        states.put("0839","四川 广元");
        states.put("0851","贵州 贵阳");
        states.put("0852","贵州 遵义");
        states.put("0853","贵州 安顺");
        states.put("0855","贵州 苗族侗族自治州");
        states.put("0856","贵州 铜仁");
        states.put("0857","贵州 毕节");
        states.put("0858","贵州 六盘水");
        states.put("0870","云南 昭通");
        states.put("0871","云南 昆明");
        states.put("0872","云南 大理白族自治州");
        states.put("0873","云南 红河哈尼族彝族自治州");
        states.put("0874","云南 曲靖");
        states.put("0875","云南 保山");
        states.put("0876","云南 文山壮族苗族自治州");
        states.put("0877","云南 玉溪");
        states.put("0878","云南 楚雄彝族自治州");
        states.put("0879","云南 思茅");
        states.put("0883","云南 临沧");
        states.put("0886","云南 怒江傈僳族自治州");
        states.put("0887","云南 迪庆藏族自治州");
        states.put("0888","云南 丽江");
        states.put("0890","海南 儋州");
        states.put("0891","西藏");
        states.put("0892","西藏 日喀则");
        states.put("0893","西藏 山南");
        states.put("0894","西藏 林芝");
        states.put("0895","西藏 昌都");
        states.put("0896","西藏 那曲");
        states.put("0897","西藏 阿里");
        states.put("0898","海南");
        states.put("0899","海南 三亚");
        states.put("0901","新疆 额敏县");
        states.put("0902","新疆 巴里坤哈萨克自治县");
        states.put("0903","新疆 策勒县");
        states.put("0906","新疆 阿勒泰市");
        states.put("0908","新疆 克孜勒苏柯尔克孜自治州");
        states.put("0909","新疆 博尔塔拉蒙古自治州");
        states.put("0910","陕西 咸阳");
        states.put("0911","陕西 延安");
        states.put("0912","陕西 榆林");
        states.put("0913","陕西 渭南");
        states.put("0914","陕西 商洛");
        states.put("0915","陕西 安康");
        states.put("0916","陕西 汉中");
        states.put("0917","陕西 宝鸡");
        states.put("0919","陕西 铜川");
        states.put("0930","甘肃 临夏");
        states.put("0931","甘肃 兰州");
        states.put("0932","甘肃 定西");
        states.put("0933","甘肃 平凉");
        states.put("0934","甘肃 庆阳");
        states.put("0935","甘肃 武威");
        states.put("0936","甘肃 张掖");
        states.put("0937","甘肃 嘉峪关");
        states.put("0938","甘肃 天水");
        states.put("0939","甘肃 陇南");
        states.put("0943","甘肃 白银");
        states.put("0951","宁夏 银川");
        states.put("0952","宁夏 大武口");
        states.put("0953","宁夏 吴忠");
        states.put("0954","宁夏 固原");
        states.put("0970","青海 海北藏族自治州");
        states.put("0971","青海 西宁");
        states.put("0972","青海 海东");
        states.put("0973","青海 黄南藏族自治州");
        states.put("0974","青海 海南藏族自治州");
        states.put("0975","青海 果洛藏族自治州");
        states.put("0976","青海 玉树藏族自治州");
        states.put("0977","青海 海西蒙古族藏族自治州");
        states.put("0978","青海 海北藏族自治州");
        states.put("0979","青海 海西蒙古族自治州");
        states.put("0990","新疆 克拉玛依");
        states.put("0991","新疆 乌鲁木齐");
        states.put("0992","新疆 乌苏市");
        states.put("0993","新疆 伊犁哈萨克自治州");
        states.put("0994","新疆 昌吉回族自治州");
        states.put("0995","新疆 鄯善县");
        states.put("0996","新疆 巴音郭楞蒙古自治州");
        states.put("0997","新疆 阿合奇县");
        states.put("0998","新疆 巴楚县");
        states.put("0999","新疆 察布查尔锡伯自治县");
    }
	
	public static HashMap<String, String> getStates() {
		return states;
	}
}
